<?php
namespace Login;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Album\View\Helper\RequestHelper;
use Login\Form\LoginForm;

//add this for Table data getway
use Login\Model\Login;
use Login\Model\UsersTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

//add this for SMTP transport
use Zend\ServiceManager\ServiceManager;
use Zend\Mail\Transport\Smtp;
use Zend\Mail\Transport\SmtpOptions;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    public function getServiceConfig(){
    	return array(
    		'factories'=>array(
    			'Login\Model\UsersTable'=>function ($sm){
    				$tableGateway	=	$sm->get('UserTableGateway');
    				$table	=	new UsersTable($tableGateway);
    				return $table;
    			},
    			'UsersTableGateway'=>function($sm){
    				$dbAdapter	=	$sm->get('Zend\Db\Adapter\Adapter');
    				$resultSetPrototype	=	new RequestSet();
    				$resultSetPrototype->setArrayObjectPrototype(new Login());
    				return new TableGateway('users',$dbAdapter,null,$resultSetPrototype);
    			},
    			'Mail.transport'	=> function(ServiceManager $serviceManager){
    				$config	=	$serviceManager->get('Config');
    				$transport	=	new Smtp();
    				$transport->setOptions(new SmtpOptions($config['mail']['transport']['options']));
    				return $transport;
    			}
    		),
    		'AuthService' => function($sm) {
    			$dbAdapter		=	$sm->get('Zend\Db\Adapter\Adapter');
    			$AuthAdapter	=	new	DbTableAuthAdapter(
    					$dbAdapter,
    					'users',
    					'email',
    					'password'
    					//,"MD5(?) AND active=1"
    			);
    			$authService = new Zend\Authentication\AuthenticationService();
    			$authService->setAdapter($AuthAdapter);    		
    			return $authService;
    		},
    	);
    }
}
